package com.gitee.wsl.compose.chart.polar

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import com.gitee.wsl.compose.chart.polar.axis.AngularAxisModel
import com.gitee.wsl.compose.chart.polar.axis.FloatRadialAxisModel
import com.gitee.wsl.compose.chart.polar.model.PolarPoint
import com.gitee.wsl.compose.chart.polar.axis.toPolarAngle
import com.gitee.wsl.compose.ui.container.hoverable.HoverableElementAreaScope
import com.gitee.wsl.mathematics.coordinate.polar.ext.polarToCartesian
import kotlin.math.min

/**
 * Scope for plot series content placed on a [PolarGraph].
 */
 interface PolarGraphScope<T> : HoverableElementAreaScope {
    /**
     * Provides the [FloatRadialAxisModel] for the plot to scale coordinates.
     */
     val radialAxisModel: FloatRadialAxisModel

    /**
     * Provides the [AngularAxisModel] for the plot to scale coordinates.
     */
     val angularAxisModel: AngularAxisModel<T>

    /**
     * Transforms the provided [point] from polar plot axis coordinates to cartesian coordinates for on-screen
     * drawing, provided a [size] of the drawing area.
     */
     fun polarToCartesian(point: PolarPoint<Float, T>, size: Size): Offset

}

class PolarGraphScopeImp<T>(
    override val radialAxisModel: FloatRadialAxisModel,
    override val angularAxisModel: AngularAxisModel<T>,
    hoverableElementAreaScope: HoverableElementAreaScope
):PolarGraphScope<T> , HoverableElementAreaScope by hoverableElementAreaScope{
    override fun polarToCartesian(point: PolarPoint<Float, T>, size: Size): Offset {

        // Transform the angle to screen coordinates.
        val theta = angularAxisModel.toPolarAngle(angularAxisModel.computeOffset(point.theta))

        val r = min(size.width / 2, size.height / 2) * radialAxisModel.computeOffset(point.r)
        return polarToCartesian(r, theta)

        //return polarToCartesianPlot(point, angularAxisModel, radialAxisModel, size)
    }

}


/*
private fun <T> polarToCartesianPlot(
    point: PolarPoint<Float, T>,
    angularAxisModel: AngularAxisModel<T>,
    radialAxisModel: FloatRadialAxisModel,
    size: Size
): Offset {
    // Transform the angle to screen coordinates.
    val theta = angularAxisModel.toPolarAngle(angularAxisModel.computeOffset(point.theta))

    val r = min(size.width / 2, size.height / 2) * radialAxisModel.computeOffset(point.r)
    return polarToCartesian(r, theta)
}*/
